package com.hubujun.driverlicense;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Process;

import com.hubujun.driverlicense.activity.BaseActivity;
import com.hubujun.driverlicense.activity.EntryPointActivity;
import com.hubujun.driverlicense.core.Trigger;
import com.hubujun.driverlicense.core.TriggerExecutor;
import com.hubujun.driverlicense.util.Ln;
import com.hubujun.driverlicense.util.MobileUtils;


public class DLAppMgr {

    private static final String SHORT_CUT = "shortcut_installed";

    public static final String ABSOLUTELY_EXIT = "absolutely_exit";

    private static DLAppMgr instance;

    private boolean activated;

    private boolean curActivatedState;

    private BaseActivity mCurTopActivity;

    private Trigger activatedTrigger = new Trigger(DLApplication.mMainHandler.getLooper(), new TriggerExecutor() {

        @Override
        public boolean execute() {
            if (curActivatedState == activated) {
                Ln.d("status not changed, cur=%s", curActivatedState);
            } else {
                curActivatedState = activated;
                if (curActivatedState) {
                    Ln.d("[ACTIVATED MODE]");
//                   setForeground(true);
                } else {
                    Ln.d("[DEACTIVATED MODE]");
//                   setForeground(false);
                }
            }
            return true;
        }

    }, false);

    private DLAppMgr() {
        activated = false;
        curActivatedState = false;
    }

    public static void setAbsolutelyExit(Context context) {
        Ln.d("DLAppMgr setAbsolutelyExit");
        Intent intent = new Intent(context, EntryPointActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(ABSOLUTELY_EXIT, true);
        intent.putExtra("exit_and_view", (Intent) null);
        if (!(context instanceof Activity))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 设置是否激活
     *
     * @param activated
     */
    public static void setActivatedState(boolean activated) {
        getInstance().activated = activated;
        getInstance().activatedTrigger.delayed(1000);
    }

    public static void setCurTopActivity(BaseActivity baseActivity) {
        getInstance().mCurTopActivity = baseActivity;
    }

    public static BaseActivity getCurTopActivity() {
        if(getInstance().activated){
            return getInstance().mCurTopActivity;
        }

        Ln.d("yks getCurTopActivity return null");
        return null;
    }

    /**
     * 通过广播添加快捷方式
     */
    public static void addShortCut() {
        SharedPreferences sp = ApplicationContext.getContext().getSharedPreferences(ApplicationContext.getPreferencesName(), 0);
        if (!sp.getBoolean(SHORT_CUT, false)) {
            MobileUtils.createShortcut(ApplicationContext.getContext(), EntryPointActivity.class.getName());
            sp.edit().putBoolean(SHORT_CUT, true).commit();
        }
    }

    /**
     * 是否为激活模式（activity运行为激活模式）
     *
     * @return
     */
    public static boolean isActivated() {
        return getInstance().curActivatedState;
    }

    public static void processExit() {
        Context context = ApplicationContext.getContext();
        Process.killProcess(Process.myPid());
    }

    private static DLAppMgr getInstance() {
        if (instance == null)
            instance = new DLAppMgr();
        return instance;
    }

}
